articles

Home / DeveloperSection / Articles / BroadcastReceiver in Android

BroadcastReceiver in Android

Manoj Pandey4907 05-Mar-2015

A broadcast receiver is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

Broadcast intents are Intent objects that are broadcast via a call to the sendBroadcast(), sendStickyBroadcast() or sendOrderedBroadcast() methods of the Activity class (the latter being used when results are required from the broadcast). In addition to providing a messaging and event system between application components, broadcast intents are also used by the Android system to notify interested applications about key system events (such as the external power supply or headphones being connected or disconnected).

Several system events are defined as final static fields in the Intent class. Other Android system classes also define events, e.g., the TelephonyManager defines events for the change of the phone state.

Intent.ACTION_BOOT_COMPLETED-:

Boot completed. Requires the android.permission.RECEIVE_BOOT_COMPLETED permission.

Intent.ACTION_POWER_CONNECTED-:

Power got connected to the device.

Intent.ACTION_POWER_DISCONNECTED-:

Power got disconnected to the device.

Intent.ACTION_BATTERY_LOW-:  

Triggered on low battery. Typically used to reduce activities in your app which consume power.

Intent.ACTION_BATTERY_OKAY-:

Battery status good again.

Here is a sample which help you to clear the concepts of BroadCastReceiver in Android

1.      Create an Android project.

2.      Create an IncommingCallReceiver class and extends with BroadcastReceiver , and add following code in this class

 

package com.example.callstatebroadcastreceiver;

 

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.telephony.TelephonyManager;

import android.widget.Toast;

 

public class IncommingCallReceiver extends BroadcastReceiver {

     Context context;

 

     @Override

     public void onReceive(Context context, Intent intent) {

           try {

                String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

                // On InComing Call

                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

                     Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();

                }

                // On Dial call

             if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

                     Text(context, "Call Recieved", Toast.LENGTH_LONG)

                                .show();

                }

                // On cancel or disconnect call

                if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

           Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG)

                                .show();

                }

           } catch (Exception e) {

               

           }

     }

}

3.      Add permission and define receiver in Manifest.xml file

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.callstatebroadcastreceiver"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

 

 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

    android:name="com.example.callstatebroadcastreceiver.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

          <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

 

        <receiver

            android:name=".IncommingCallReceiver"

            android:enabled="true" >

            <intent-filter>

           <action android:name="android.intent.action.PHONE_STATE" />

            </intent-filter>

        </receiver>

    </application>

 

</manifest>

4. No need to add extra code in MainActivity.class

 

package com.example.callstatebroadcastreceiver;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if // it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     } 

Now run your application in android device

On Incoming call

BroadcastReceiver in Android

On Dial call

BroadcastReceiver in Android

 

 

 

 

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By